package hashTable;

import lombok.Data;

import java.util.ArrayList;
import java.util.Scanner;

public class MyHashTable {
    public static void main(String[] args) {
        HashTable ht = new HashTable(7);
        // 写一个简单菜单
        String key  = "";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("add:添加雇员");
            System.out.println("list:显示雇员");
            System.out.println("search:查找雇员id");
            System.out.println("delete:删除雇员id");
            System.out.println("exit:退出");
            key = scanner.next();
            switch (key){
                case "add":
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name  = scanner.next();
                    Employee emp = new Employee(id,name);
                    ht.add(emp);
                    break;
                case "list":
                    ht.display();
                    break;
                case "search":
                    System.out.println("输入id");
                    int K = scanner.nextInt();
                    ht.findById(K);
                    break;
                case "delete":
                    System.out.println("输入id");
                    int k = scanner.nextInt();
                    ht.delete(k);
                    break;
                case"exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }
    }
}
class HashTable{
    private ArrayList<EmLinkedList> list;
    private int size;

    public HashTable(int size) {
        list = new ArrayList<>(size);
        this.size = size;
        for (int i = 0; i < size; i++) {
            list.add(new EmLinkedList());
        }
    }
    private int hash(int id){
        return id%size;
    }
    public void add(Employee emp){
        int id = hash(emp.id);
        list.get(id).add(emp);
    }
    public void display(){
        for (int i = 0; i < size; i++) {
            list.get(i).display(i+1);
        }
    }
    // 根据id查找雇员
    public void findById(int id){
        int EmpId = hash(id);
        Employee res = list.get(EmpId).getById(id);
        if (null==res){
            System.out.println("此id"+id+"不存在");
        }else {
            System.out.printf("==>==>id=%d name=%s\t",res.id,res.name);
        }
    }
    public void delete(int id){
        int EmpId = hash(id);
        boolean status = list.get(EmpId).delete(id);
        if (status==false){
            System.out.println("没有此人");
        }else {
            System.out.println("删除成功");
        }
    }
}
// 表示一个雇员
class Employee {
    public int id;
    public String name;
    public Employee next;
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
// 表示链表
@Data
class EmLinkedList{
    private Employee head;
    private int size;
    public void add(Employee emp){
        if (size==0){
            head = emp;
            size++;
            return ;
        }
        Employee curEmp = head;
        while (true){
            if (curEmp.next==null){
                break;
            }
            curEmp = curEmp.next;
        }
        curEmp.next = emp;
        size++;
    }
    public void display(int no){
        if (size==0){
            System.out.println("当前"+no+"链表为空");
            return;
        }
        Employee curEmp = head;
        while (true){
            System.out.printf("==%d==>==>id=%d name=%s\t",no,curEmp.id,curEmp.name);
            if (curEmp.next==null){
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }
    // 在次链表中找到具体id
    public Employee getById(int id){
        if (size==0){
            return null;
        }
        Employee curEmp = head;
        while (true){
            if (curEmp.id == id){
                return curEmp;
            }
            if (curEmp.next == null){
                return null;
            }
            curEmp =curEmp.next;
        }
    }
    // 根据id删除
    public boolean delete(int id){
        if (size==0){
            return false;
        }
        if (size==1 && head.id==id) {
            head = null;
            size--;
            return true;
        }
        if (size>1 && head.id==id){
           head = head.next;
            size--;
            return true;
        }
        Employee curEmp = head;
        Employee tmp;
        while (true){
             tmp= curEmp.next;
            if (tmp.id==id){
                curEmp.next = tmp.next;
                tmp.next = null;
                size--;
                return true;
            }else {
                curEmp = curEmp.next;
                if (tmp==null){
                    return false;
                }
            }
        }
    }
}
